home *** CD-ROM | disk | FTP | other *** search
/ .net (French) 1996 November / .net Magazine (FR) - Issue 01 - Nov 1996.iso / mac / Outils / NetCacheBack 0.9d6 ƒ / NetCacheBack source < prev    next >
Text File  |  1996-05-12  |  12KB  |  394 lines

  1. -- Netscape¬ Cache Back 0.9d6
  2. -- Copyright ⌐ 1995-96 Mizutori Tetsuya, May 12, 1996
  3.  
  4. -- global constants
  5. set gCurrentFolder to "" as string
  6. set gCacheRecordList to {} as list
  7. -- global constants for NetCacheBack 0.9d6
  8. set gTraceLog to true as boolean
  9. set gVerbose to true as boolean
  10. set gOverride to false as boolean
  11. set gFormList to {} as list
  12. set gTracingNow to 0 as number
  13. set gTraceLogFile to "NetCacheBack log"
  14.  
  15. tell me to open {}
  16.  
  17. --
  18. on open (docList)
  19.     local currItem
  20.     --
  21.     if docList is {} then set docList to {(choose file)}
  22.     --
  23.     --NetCacheBack 0.9d6
  24.     tell me to initGlobals()
  25.     tell me to getPreferences()
  26.     --
  27.     tell me to setupConstants by the first item of docList
  28.     --
  29.     repeat with currItem in docList
  30.         tell me to moveCacheFile against currItem
  31.     end repeat
  32. end open
  33.  
  34.  
  35. --NetCacheBack 0.9d6
  36. to initGlobals()
  37.     global gCurrentFolder, gCacheRecordList
  38.     global gTraceLog, gVerbose, gOverride
  39.     global gFormList
  40.     global gTracingNow, gTraceLogFile
  41.     --
  42.     set gCurrentFolder to ""
  43.     set gCacheRecordList to {}
  44.     --
  45.     set gTraceLog to true
  46.     set gVerbose to true
  47.     set gOverride to false
  48.     set gFormList to {}
  49.     set gTracingNow to 0
  50.     set gTraceLogFile to "NetCacheBack log"
  51.     --
  52. end initGlobals
  53.  
  54.  
  55. to setupConstants by currentFile
  56.     global gCurrentFolder, gCacheRecordList, gTraceLogFile
  57.     local refFile, cacheRecordList, cachelog, ccacheLog, viewBy
  58.     --
  59.     tell application "Finder" to get the container of currentFile
  60.     copy the result to gCurrentFolder
  61.     --
  62.     --NetCacheBack 0.9d6
  63.     copy ((gCurrentFolder as string) & gTraceLogFile) to gTraceLogFile
  64.     --
  65.     copy ((gCurrentFolder as string) & "Cache log") to cachelog
  66.     copy ((gCurrentFolder as string) & "CCache log") to ccacheLog
  67.     --
  68.     tell application "Finder"
  69.         open the gCurrentFolder
  70.         set viewBy to the view of the gCurrentFolder
  71.         if exists the (alias cachelog) then
  72.             if exists the (alias ccacheLog) then
  73.                 if the modification date of the (alias ccacheLog) comes after ┬
  74.                     the modification date of the (alias cachelog) then
  75.                     tell application "NetCacheResolver"
  76.                         open {(alias ccacheLog)}
  77.                         quit
  78.                     end tell
  79.                 end if
  80.             end if
  81.             try
  82.                 set refFile to open for access (alias cachelog)
  83.                 set cacheRecordList to read refFile as list using delimiters return
  84.                 close access refFile
  85.                 --
  86.                 tell application "NetCacheResolver"
  87.                     if the viewBy is byDate then
  88.                         sort cacheRecordList by date
  89.                     else if the viewBy is bySize then
  90.                         sort cacheRecordList by size
  91.                     else
  92.                         sort cacheRecordList by name
  93.                     end if
  94.                     copy the result to gCacheRecordList
  95.                     quit
  96.                 end tell
  97.             on error
  98.                 tell me to errorMessage for "Can╒t read ╥Cache log╙ file"
  99.             end try
  100.         else if exists the (alias ccacheLog) then
  101.             tell application "NetCacheResolver"
  102.                 if the viewBy is byDate then
  103.                     resolve {(alias ccacheLog)} with sorted by date
  104.                 else if the viewBy is bySize then
  105.                     resolve {(alias ccacheLog)} with sorted by size
  106.                 else
  107.                     resolve {(alias ccacheLog)} with sorted by name
  108.                 end if
  109.                 copy the result to gCacheRecordList
  110.                 quit
  111.             end tell
  112.         else
  113.             tell me to errorMessage for "Can╒t find ╥Cache log╙ or ╥CCache log╙ file"
  114.         end if
  115.     end tell
  116. end setupConstants
  117.  
  118.  
  119. to moveCacheFile against cacheFile
  120.     global gCurrentFolder, gCacheRecordList
  121.     local refFile
  122.     local cacheRecord, cachefolder, cacheName
  123.     local url, urlFolder, urlName
  124.     local n
  125.     --
  126.     tell application "Finder"
  127.         copy the container of cacheFile to cachefolder
  128.         copy the name of cacheFile to cacheName
  129.     end tell
  130.     --
  131.     set n to 1
  132.     repeat with cacheRecord in gCacheRecordList
  133.         if cacheName is in the cacheRecord then
  134.             tell me to renameCacheFile(cachefolder, cacheName, cacheRecord)
  135.             set the item n of gCacheRecordList to ""
  136.             exit repeat
  137.         end if
  138.         set n to n + 1
  139.     end repeat
  140.     --
  141. end moveCacheFile
  142.  
  143.  
  144. to renameCacheFile(cachefolder, cacheName, cacheRecord)
  145.     global gCurrentFolder, gCacheRecordList
  146.     global gTraceLog, gVerbose, gOverride
  147.     local oldDelimiters, cacheItems, cacheFilePath
  148.     local url, urlFolder, urlName, urlItems
  149.     local targetFolderPath, fileType
  150.     --
  151.     copy the text item delimiters to oldDelimiters
  152.     --
  153.     tell me to set the text item delimiters to {tab}
  154.     copy every text item in cacheRecord to cacheItems
  155.     copy the 6th item of cacheItems to fileType
  156.     copy the 5th item of cacheItems to url
  157.     --
  158.     tell me to set the text item delimiters to {"/"}
  159.     copy every text item in url to urlItems
  160.     copy (items 3 through -2 in urlItems) to urlFolder
  161.     copy the last item in urlItems to urlName
  162.     tell me to set the text item delimiters to {""}
  163.     if urlName is "" then set the urlName to "index.html"
  164.     if urlName contains "#" then ┬
  165.         copy characters 1 thru ((offset of "#" in urlName) - 1) in urlName as string to urlName
  166.     --
  167.     tell me to set the text item delimiters to {":"}
  168.     copy ((cachefolder as string) & cacheName) to cacheFilePath
  169.     copy ((gCurrentFolder as string) & (urlFolder as string)) to targetFolderPath
  170.     --
  171.     tell me to attachFolder at targetFolderPath as string
  172.     tell me to setFileAttribute of (alias cacheFilePath) by fileType
  173.     --
  174.     copy oldDelimiters to the text item delimiters
  175.     --
  176.     tell application "Finder"
  177.         try
  178.             move alias cacheFilePath to the folder targetFolderPath
  179.             --NetCacheBack 0.9d6
  180.             if not gOverride then
  181.                 tell me to generateFileName by urlName
  182.                 copy the result to urlName
  183.             end if
  184.             if the file urlName in the folder targetFolderPath exists then
  185.                 if not gOverride then
  186.                     --NetCacheBack 0.9d6
  187.                     tell me to traceLog for "╥" & targetFolderPath & ":" & urlName & "╙ was replaced."
  188.                     delete the file urlName in the folder targetFolderPath
  189.                 else
  190.                     error
  191.                 end if
  192.             end if
  193.             set the name of file cacheName in the folder targetFolderPath to urlName
  194.         on error
  195.             --NetCacheBack 0.9d6
  196.             tell me to traceLog for "Can╒t move ╥" & cacheName & "╙ to " & targetFolderPath & ":" & urlName
  197.             --NetCacheBack 0.9d6
  198.             if gVerbose then ┬
  199.                 display dialog "Can╒t move ╥" & cacheName & "╙ to " & targetFolderPath & ":" & urlName ┬
  200.                     buttons {"OK"} default button 1 with icon caution
  201.         end try
  202.     end tell
  203. end renameCacheFile
  204.  
  205.  
  206. to attachFolder at pathName -- as string
  207.     local listFolders, numbFolders, parentFolder, targetFolder
  208.     --
  209.     tell application "Finder"
  210.         if pathName is "" then
  211.             return path to desktop -- case of the Desktop
  212.         else if the folder pathName exists then
  213.             return the folder pathName -- case of the Folder
  214.         else if the disk pathName exists then
  215.             return the disk pathName -- case of the Volume
  216.         else
  217.             --beep
  218.             tell me to set the text item delimiters to {":"}
  219.             copy every text item of pathName to the listFolders
  220.             copy (the count of items in the listFolders) to the numbFolders
  221.             if listFolders ends with {""} then ┬
  222.                 copy the numbFolders - 1 to the numbFolders
  223.             copy the item numbFolders in the listFolders to targetFolder
  224.             copy (every item in (items 1 thru (numbFolders - 1) in the listFolders)) & {""} ┬
  225.                 to parentFolder
  226.             tell me to attachFolder at the parentFolder as string
  227.             make new folder at the result with properties {name:targetFolder}
  228.             return the result
  229.         end if
  230.     end tell
  231. end attachFolder
  232.  
  233.  
  234. -- NetCacheBack 0.9d6
  235. to generateFileName by theFilename
  236.     local thePos, changed, theFilename0
  237.     local oldDelimiters
  238.     --
  239.     copy the text item delimiters to oldDelimiters
  240.     tell me to set the text item delimiters to {""}
  241.     --
  242.     copy theFilename to theFilename0
  243.     set changed to false
  244.     --
  245.     -- truncate file length in 31 characters
  246.     set thePos to (count of theFilename)
  247.     if thePos is greater than 31 then
  248.         copy ┬
  249.             (characters 1 thru 25 in theFilename as string) & ┬
  250.             (characters (thePos-5) thru (thePos) in theFilename as string) ┬
  251.             to theFilename
  252.         set changed to true
  253.     end if
  254.     --
  255.     -- zap ":" in filename
  256.     repeat
  257.         set thePos to offset of ":" in theFilename
  258.         if thePos is 0 then exit repeat
  259.         copy ┬
  260.             (characters 1 thru (thePos - 1) in theFilename as string) & "_" & ┬
  261.             (characters (thePos + 1) thru (count of theFilename) in theFilename as string) ┬
  262.                 to theFilename
  263.         set changed to true
  264.     end repeat
  265.     --
  266.     if changed then ┬
  267.         tell me to traceLog for "╥" & theFilename0 & "╙ was truncated or zapped to ╥" & theFilename & "╙."
  268.     --
  269.     copy oldDelimiters to the text item delimiters
  270.     --
  271.     return theFilename as string
  272. end generateFileName
  273.  
  274.  
  275. -- NetCacheBack 0.9d5
  276. --to setFileAttribute of fileRef by fileType
  277. --    tell application "Finder"
  278. --        if fileType contains "text/html" then
  279. --            set the file type of fileRef to "TEXT"
  280. --            set the creator type of fileRef to "MOSS"
  281. --        else if fileType contains "text/plain" then
  282. --            set the file type of fileRef to "TEXT"
  283. --            set the creator type of fileRef to "ttxt"
  284. --        else if fileType contains "image/gif" then
  285. --            set the file type of fileRef to "GIFf"
  286. --            set the creator type of fileRef to "JVWR"
  287. --        else if fileType contains "image/jpeg" then
  288. --            set the file type of fileRef to "JPEG"
  289. --            set the creator type of fileRef to "JVWR"
  290. --        end if
  291. --    end tell
  292. --end setFileAttribute
  293.  
  294.  
  295. -- NetCacheBack 0.9d6
  296. -- The file types are read from "NetCacheBack Pref" file.
  297. to setFileAttribute of fileRef by fileType
  298.     global gTraceLog, gVerbose, gOverride, gFormList
  299.     local theFormPair
  300.     --
  301.     tell application "Finder"
  302.         repeat with theFormPair in gFormList
  303.             if the theFormPair begins with fileType then
  304.                 set the file type of fileRef to the 2nd item of theFormPair
  305.                 set the creator type of fileRef to the 3rd item of theFormPair
  306.                 exit repeat
  307.             end if
  308.         end repeat
  309.     end tell
  310. end setFileAttribute
  311.  
  312.  
  313. -- NetCacheBack 0.9d6
  314. to getPreferences()
  315.     global gTraceLog, gVerbose, gOverride, gFormList
  316.     local prefName, prefFile, prefList, theItem
  317.     --
  318.     -- read preference data from pref file
  319.     set prefName to ((path to preferences folder) as string) & "NetCacheBack Prefs"
  320.     set prefFile to open for access (file prefName)
  321.     set prefList to read prefFile as list using delimiters return
  322.     close access prefFile
  323.     --
  324.     -- set variable status due to preference data
  325.     ignoring case and white space
  326.         repeat with theItem in prefList
  327.             if theItem starts with ";" then
  328.                 -- the line begins with ";" is a comment
  329.             else if theItem starts with "tracelog=" then
  330.                 set gTraceLog to ("=yes" is in theItem) as boolean
  331.             else if theItem starts with "verbose=" then
  332.                 set gVerbose to ("=yes" is in theItem) as boolean
  333.             else if theItem starts with "override=" then
  334.                 set gOverride to ("=yes" is in theItem) as boolean
  335.             else if theItem starts with "form=" then
  336.                 tell me to getFormPair from theItem
  337.                 copy (gFormList & the result) as list to gFormList
  338.             end if
  339.         end repeat
  340.     end ignoring
  341. end getPreferences
  342.  
  343.  
  344. -- NetCacheBack 0.9d6
  345. to getFormPair from theString
  346.     local thePair, thePairList, oldDelimiters
  347.     --
  348.     copy the text item delimiters to oldDelimiters
  349.     --
  350.     tell me to set the text item delimiters to {""}
  351.     copy characters ((offset of "=" in theString) + 1) thru (count of theString) in theString as string ┬
  352.         to thePair
  353.     if thePair ends with "\"" then
  354.         copy characters ((offset of "\"" in thePair) + 1) thru ((count of thePair) - 1) in thePair as string ┬
  355.             to thePair
  356.     end if
  357.     --
  358.     tell me to set the text item delimiters to {tab}
  359.     copy every text item in thePair to thePairList
  360.     --
  361.     copy oldDelimiters to the text item delimiters
  362.     --
  363.     return {thePairList} as list
  364. end getFormPair
  365.  
  366.  
  367. -- NetCacheBack 0.9d6
  368. on traceLog for errMessage
  369.     global gTraceLog, gTracingNow, gTraceLogFile
  370.     local refFile
  371.     --
  372.     if not gTraceLog then return
  373.     set gTracingNow to (gTracingNow + 1)
  374.     --
  375.     try
  376.         set refFile to (open for access file gTraceLogFile with write permission)
  377.         if gTracingNow is equal to 1 then
  378.             set eof refFile to 0
  379.             write "### NetCacheBack Log on " & (current date) & return to refFile
  380.         end if
  381.         write "[" & (gTracingNow as string) & "] " & errMessage & return ┬
  382.         to refFile starting at eof
  383.         close access refFile
  384.         --
  385.     on error
  386.         -- do nothing
  387.     end try
  388. end traceLog
  389.  
  390.  
  391. on errorMessage for errMessage
  392.     error errMessage
  393. end errorMessage
  394.